//
//  MHCircleHomeModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/13.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHCircleHomeViewModel.h"
#import "MHCircleHomeModel.h"
#import "MHHomeCirleItemViewModel.h"
#import "MHSetCirclePropertyViewModel.h"
#import "MHCircleInfo.h"
#import "MHMomentViewModel.h"
#import "MHMomentViewModel.h"
#import "MLCircleInfoMainPageViewModel.h"
#import "MHTestViewModel.h"

@interface MHCircleHomeViewModel ()
@property (nonatomic, readwrite, strong) RACCommand *rightButtonCommand;
@end
@implementation MHCircleHomeViewModel

- (void)initialize
{
    [super initialize];
    self.title =  LSSTRING("圈子");
    
    self.rightImageStr = @"";
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    /// 没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = NO;
    
    
    @weakify(self);
    //圈子列表
    RAC(self, circles) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, circles) map:^(NSArray * circles) {
        @strongify(self)
        return [self dataSourceWithCircles:circles];
    }];
    
    self.rightButtonCommand  = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        MHMomentViewModel *viewModel = [[MHMomentViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@1,MHViewModelTitleKey:@"动态"}];
          viewModel.userId = [MHHTTPService sharedInstance].currentUser.userId;
          viewModel.isRecent = YES;
            [self.services pushViewModel:viewModel animated:YES];
      
        return [RACSignal empty];
    }];
    
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
        @strongify(self);
        /// 这里只是测试
        if (indexPath.item != self.circles.count) {
             MHCircleHomeModel *circle = self.circles[indexPath.row];
            MLCircleInfoMainPageViewModel *viewModel = [[MLCircleInfoMainPageViewModel alloc] initWithServices:self.services params:@{MHViewModelIDKey:@(circle.autoId)}];
            [self.services pushViewModel:viewModel animated:YES];
//            MHTestViewModel *viewModel = [[MHTestViewModel alloc] initWithServices:self.services params:@{MHViewModelTitleKey:@"22"}];
//            //        /// 执行push or present
//                [self.services pushViewModel:viewModel animated:YES];
        }else
        {
           
            MHSetCirclePropertyViewModel *viewModel = [[MHSetCirclePropertyViewModel alloc] initWithServices:self.services params:nil];
            [self.services presentViewModel:viewModel animated:YES completion:nil];
        }
        
//
        return [RACSignal empty];
    }];
    
}



/// 请求数据
- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSArray * (^mapCircles)(NSArray *) = ^(NSArray *products) {
        if (page == 1) {
            /// 下拉刷新
        } else {
            /// 上拉加载
            products = @[(self.circles ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
        }
        return products;
    };
    return  [[[self.services.client fetchHomeCircleDataWithPageNum:1 pageSize:self.perPage] mh_parsedResults] map:mapCircles];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithCircles:(NSArray *)circles {
    MHHomeCirleItemViewModel *addViewModel = [[MHHomeCirleItemViewModel alloc] initWithCircle:[MHCircleHomeModel new]];
    if (MHObjectIsNil(circles) || circles.count == 0) return @[addViewModel];
    NSMutableArray *viewModels = [[circles.rac_sequence map:^(MHCircleHomeModel *homeCircle) {
        MHHomeCirleItemViewModel *viewModel = [[MHHomeCirleItemViewModel alloc] initWithCircle:homeCircle];
        return viewModel;
    }].array mutableCopy];
    [viewModels appendObject:addViewModel];
    return viewModels ?: @[] ;
}




@end
